%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
clear all; close all; clc;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Modify main_str to match path on your machine
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
main_str = 'C:\Users\Marco\Dropbox\Housing_Brainstorming\Information_disclosure';
% main_str = 'L:\agargano\Dropbox\Housing_Brainstorming\Information_disclosure';

load_str = [main_str,'\Replication_code_RFS\Model\Output\Last'];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Select whether bidders calculate ``objective'' or ``subjective'' surplus
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% sel_unb = 1; % ``objective surplus'': based on pre-shock valuations and true private values
sel_unb = 0;  % ``subjective surplus'': based on post-shock valuations and noisy private values

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Just Load Some Matrix Sizes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load([load_str,'\EndowBiasInd\equilibrium_step1_last_endow_mu1_biasstd0.mat'],...
      'start_bid_scale','n_bidders_all_vec','n_start',...
      'grid_mat','n_grid');
i_all = size(n_bidders_all_vec,2);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Choose level of overbidding (\sigma_{\psi})
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
endow_sel     = 0.09;
% endow_sel     = 0.04;
% endow_sel     = 0.03;

% Select available values of \sigma_{\epsilon}
ind_sel       = round(grid_mat(:,1),2)==endow_sel;
grid_sel_mat  = grid_mat(ind_sel,:);
n_grid_sel    = size(grid_sel_mat,1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Solve Model for Different Levels of alpha

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
q_vec = start_bid_scale-1; % vector of values of alpha

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
max_num           = 12; % maximum number of bidders
min_num           = 3;  % minimum number of bidders 
tot_num           = max_num - min_num + 1; % range of matched bidders

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
P_vec        = (0:0.01:1)';  % vector of values of the probability of determining private value (q)
n_p          = size(P_vec,1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n_new_sim    = 5000000; % number of draws from mixing probability in simulation 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
J            = n_grid_sel; % number of different calibrations with varying values of \sigma_{\epsilon}, given \sigma_{\psi}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
C            = 5000; % paramter $C$ for info acq cost function

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Allocate Memory
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
eq_EbidW_mat         = nan(n_start,J);
eq_EbidW_obj_unb_mat = nan(n_start,J);
eq_EbidN_mat         = nan(n_start,J);
eq_Erev_mat          = nan(n_start,J);
eq_Esales_mat        = nan(n_start,J);
eq_Srev_mat          = nan(n_start,J);
eq_Ssales_mat        = nan(n_start,J);
eq_Psales_mat        = nan(n_start,J);
eq_NoWithd_mat       = nan(n_start,J);
eq_Nall_mat          = nan(n_start,J);
eq_p_hcost0_mat      = nan(n_start,J);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for j = 1:J

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Load auction outcomes given \sigma_{\epsilon} and \sigma_{\psi}
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
    load([load_str,'\EndowBiasInd\equilibrium_step1_last_endow_mu',num2str(grid_sel_mat(j,1)*100),...
         '_biasstd',num2str(grid_sel_mat(j,2)*100),'.mat'],...
          'Erev_mat','Esales_mat','Psales_mat','Ebidders_mat','EbidderW_mat','EbidderW_obj_unb_mat');
 

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    poolobj         = parpool('local');
   
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    parfor k = 1:n_start % loop over values of alpha

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        P_vec_sel        = P_vec; % entry probabilities vector
        CC               = abs(q_vec(k))*C; % standard info acq cost function

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        % Allocate Matrices to Parallel Loop
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%       
        Erev_mat_             = Erev_mat;
        Esales_mat_           = Esales_mat;
        Psales_mat_           = Psales_mat;
        Ebidders_mat_         = Ebidders_mat;
        EbidderW_mat_         = EbidderW_mat;
        EbidderW_obj_unb_mat_ = EbidderW_obj_unb_mat;

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        % Allocate Memory
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        eq_Nall_curr          = nan(n_p,1);
        eq_EbidW_curr         = nan(n_p,1);
        eq_EbidW_obj_unb_curr = nan(n_p,1);
        eq_EbidN_curr         = nan(n_p,1);
        eq_Erev_curr          = nan(n_p,1);
        eq_Esales_curr        = nan(n_p,1);
        eq_Psales_curr        = nan(n_p,1);
        eq_NoWithd_curr       = nan(n_p,1);       

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        
        for i = 1:n_p % loop over values of entry probability q

            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            for nn = min_num:max_num % loop over number of bidders matched to listing

    
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
                % Allocate Memory
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                EbidderWns_curr         = nan(n_new_sim,1);
                EbidderWns_obj_unb_curr = nan(n_new_sim,1); 
                EbidderNns_curr         = nan(n_new_sim,1);        
                Erev_curr               = nan(n_new_sim,1); 
                Esales_curr             = nan(n_new_sim,1);
                Psales_curr             = nan(n_new_sim,1);
                NoWithd_curr            = nan(n_new_sim,1);
                num_no_hcost_curr       = nan(n_new_sim,1);          

                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%            
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                % Simulate Decisions To Determine Private Values, and Calculate Auction Outcomes
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        
                for n = 1:n_new_sim

                    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                    % Decisions
                    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                
                    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                
                    ind_h0 = rand(nn  ,1)<=P_vec_sel(i,1); % bidders determining private value, unconditional
                    ind_hB = rand(nn-1,1)<=P_vec_sel(i,1); % bidders determining private value, conditional on bidder i entering
                    nmax   = sum(ind_h0); % number of bidders determining private value, unconditional
                    nmaxB  = sum(ind_hB)+1;  % number of bidders determining private value, conditional on bidder i entering

                    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                    if nmax > 1 % if there is more than one bidder determining private value
                        Erev_curr(n,1)         = Erev_mat_(nmax,k); % seller revenue
                        Esales_curr(n,1)       = Esales_mat_(nmax,k); % expected sales price
                        Psales_curr(n,1)       = Psales_mat_(nmax,k); % probability of sale
                        num_no_hcost_curr(n,1) = nmax; % number of bidders determining private value
                        NoWithd_curr(n,1)      = 1; % auction is not called off                    
                        EbidderNns_curr(n,1)   = Ebidders_mat_(nmax,k); % share of bidders who enter that join auction (b>entry bid) 
                    end 

                    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                
                    if nmax == 1 % if there is one bidder determining private value (auction is called off)
                        EbidderNns_curr(n,1)   = nan;                
                        Erev_curr(n,1)         = 0;                
                        Esales_curr(n,1)       = nan;
                        Psales_curr(n,1)       = 0;
                        num_no_hcost_curr(n,1) = 1;                    
                        NoWithd_curr(n,1)      = 0;
                    end

                    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                
                    if nmax == 0 % if there is no bidder determining private value (auction is called off) 
                        EbidderNns_curr(n,1)   = nan;                
                        Erev_curr(n,1)         = 0;                
                        Esales_curr(n,1)       = nan;
                        Psales_curr(n,1)       = 0;
                        num_no_hcost_curr(n,1) = 0;                    
                        NoWithd_curr(n,1)      = 0;
                    end                

                    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                  
                    if nmaxB > 1  % expected surplus for bidder i conditional on determing value, net of cost CC
                        EbidderWns_curr(n,1)          = EbidderW_mat_(nmaxB,k)     - CC; % ``subjective''   
                        EbidderWns_obj_unb_curr(n,1)  = EbidderW_obj_unb_mat_(nmaxB,k) - CC;  % ``objective''
                    elseif  nmaxB == 1 % if bidder i is the only one determing value, she pays the info acq cost, but there is no auction
                        EbidderWns_curr(n,1)          = - CC;
                        EbidderWns_obj_unb_curr(n,1)  = - CC;
                    end    


                end

                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                % Average Result by alpha
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                if nn == min_num
                    eq_Nall_curr(i)          = nanmean(num_no_hcost_curr)/tot_num;                    
                    eq_EbidW_curr(i)         = nanmean(EbidderWns_curr)*P_vec_sel(i)/tot_num;  % expected surplus for bidder i, depends on mixing probability of determining private value
                    eq_EbidW_obj_unb_curr(i) = nanmean(EbidderWns_obj_unb_curr)*P_vec_sel(i)/tot_num;  % expected surplus for bidder i, depends on mixing probability of determining private value                    
                    eq_EbidN_curr(i)         = nanmean(EbidderNns_curr)/tot_num;            
                    eq_Erev_curr(i)          = nanmean(Erev_curr)/tot_num;
                    eq_Esales_curr(i)        = nanmean(Esales_curr)/tot_num;
                    eq_Psales_curr(i)        = nanmean(Psales_curr)/tot_num;
                    eq_NoWithd_curr(i)       = nanmean(NoWithd_curr)/tot_num;                    
                else
                    eq_Nall_curr(i)          = eq_Nall_curr(i)      + (nanmean(num_no_hcost_curr)/tot_num);
                    eq_EbidW_curr(i)         = eq_EbidW_curr(i)     + (nanmean(EbidderWns_curr)*P_vec_sel(i)/tot_num);
                    eq_EbidW_obj_unb_curr(i) = eq_EbidW_obj_unb_curr(i) + (nanmean(EbidderWns_obj_unb_curr)*P_vec_sel(i)/tot_num);                    
                    eq_EbidN_curr(i)         = eq_EbidN_curr(i)     + (nanmean(EbidderNns_curr)/tot_num);            
                    eq_Erev_curr(i)          = eq_Erev_curr(i)      + (nanmean(Erev_curr)/tot_num);
                    eq_Esales_curr(i)        = eq_Esales_curr(i)    + (nanmean(Esales_curr)/tot_num);
                    eq_Psales_curr(i)        = eq_Psales_curr(i)    + (nanmean(Psales_curr)/tot_num);
                    eq_NoWithd_curr(i)       = eq_NoWithd_curr(i)   + (nanmean(NoWithd_curr)/tot_num);                    
                end     
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        
            end

        end

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        % bidder i selects, given alpha, the entry probability that maximizes her surplus
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        if sel_unb == 1 % ``objective''       
            [~,find_max]         = max(eq_EbidW_obj_unb_curr);         
        elseif sel_unb == 0 % ``subjective''       
            [~,find_max]         = max(eq_EbidW_curr);         
        end 

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
        eq_EbidW_mat(k,j)         = eq_EbidW_curr(find_max); % expected ``subjective'' surplus 
        eq_EbidW_obj_unb_mat(k,j) = eq_EbidW_obj_unb_curr(find_max); % expected ``objective'' surplus 
        eq_EbidN_mat(k,j)         = eq_EbidN_curr(find_max); % expected share of bidders joining the auction
        eq_Erev_mat(k,j)          = eq_Erev_curr(find_max); % expected seller revenue
        eq_Esales_mat(k,j)        = eq_Esales_curr(find_max); % expected sales price - seller reservation
        eq_Psales_mat(k,j)        = eq_Psales_curr(find_max);  % sales probability
        eq_NoWithd_mat(k,j)       = eq_NoWithd_curr(find_max); % expected number of bidders determining private value
        eq_p_hcost0_mat(k,j)      = P_vec_sel(find_max); % probability of determining private value
    end

    %%%%%%%%%%%%%%%%%%%%
    delete(poolobj);

    %%%%%%%%%%%%%%%%%%%%
    j

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Save

if sel_unb == 1
    save([load_str,'\Results_out_5_Nash_Loop_Unb_endowstd_',num2str(endow_sel),'.mat']);
elseif sel_unb == 0
    save([load_str,'\Results_out_5_Nash_Loop_endowstd_',num2str(endow_sel),'.mat']);
end 



